package com.scau402.system1.domain.factory;

import com.scau402.system1.domain.po.WmsInboundOrderPO;
import com.scau402.system1.domain.po.WmsInboundOrderDetailPO;
import com.scau402.system1.domain.query.WmsInboundOrderQuery;
import com.scau402.system1.domain.bo.WmsInboundOrderBO;
import com.scau402.system1.domain.bo.WmsInboundOrderDetailBO;
import com.scau402.system1.domain.vo.WmsInboundOrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 入库实体工厂
 *
 * @author
 * @create 2023/12/29 11:24
 * @description
 */
@Component
public class WmsInboundOrderFactory {

    public WmsInboundOrderPO toWmsInboundOrderPO(WmsInboundOrderQuery query) {
        return Optional.ofNullable(query)
                .map(x -> {
                    WmsInboundOrderPO wmsInboundOrderPO = new WmsInboundOrderPO();
                    BeanUtils.copyProperties(x, wmsInboundOrderPO);
                    return wmsInboundOrderPO;
                })
                .orElse(null);
    }

    public WmsInboundOrderBO toWmsInboundOrderBO(WmsInboundOrderPO wmsInboundOrderPO, List<WmsInboundOrderDetailPO> wmsInboundOrderDetailPOList) {
        return Optional.ofNullable(wmsInboundOrderPO)
                .map(x -> {
                    WmsInboundOrderBO wmsInboundOrderBO = new WmsInboundOrderBO();
                    BeanUtils.copyProperties(x, wmsInboundOrderBO);
                    wmsInboundOrderBO.setWmsInboundOrderDetailBOList(toWmsInboundOrderDetailBOList(wmsInboundOrderDetailPOList));
                    return wmsInboundOrderBO;
                })
                .orElse(null);
    }

    public List<WmsInboundOrderBO> toWmsInboundOrderBOList(List<WmsInboundOrderPO> wmsInboundOrderPOList,
                                                           Map<String, List<WmsInboundOrderDetailPO>> inboundOrderDetailPOMap) {
        return Optional.ofNullable(wmsInboundOrderPOList)
                .orElse(new ArrayList<>(0))
                .stream()
                .map(wmsInboundOrderPO -> toWmsInboundOrderBO(
                        wmsInboundOrderPO,
                        inboundOrderDetailPOMap.computeIfAbsent(wmsInboundOrderPO.getOrderCode(), k -> new ArrayList<>(0))))
                .collect(Collectors.toList());
    }

    /**
     * 转换为WmsInboundOrderDetailBO
     *
     * @param wmsInboundOrderDetailPO 入库单明细PO
     * @return
     */
    public WmsInboundOrderDetailBO toWmsInboundOrderDetailBO(WmsInboundOrderDetailPO wmsInboundOrderDetailPO) {
        return Optional.ofNullable(wmsInboundOrderDetailPO)
                .map(x -> {
                    WmsInboundOrderDetailBO wmsInboundOrderDetailBO = new WmsInboundOrderDetailBO();
                    BeanUtils.copyProperties(x, wmsInboundOrderDetailBO);
                    return wmsInboundOrderDetailBO;
                })
                .orElse(null);
    }

    public List<WmsInboundOrderDetailBO> toWmsInboundOrderDetailBOList(List<WmsInboundOrderDetailPO> wmsInboundOrderDetailPOS) {
        return Optional.ofNullable(wmsInboundOrderDetailPOS)
                .orElse(new ArrayList<>(0))
                .stream()
                .map(this::toWmsInboundOrderDetailBO)
                .collect(Collectors.toList());
    }

    /**
     * PO转换成VO
     * @param po PO
     * @return VO
     * @since 2024/2/17
     */
    public WmsInboundOrderVO POToVO(WmsInboundOrderPO po) {
        return Optional.ofNullable(po)
                .map(x->{
                    WmsInboundOrderVO vo = new WmsInboundOrderVO();
                    return vo;
                })
                .orElse(null);
    }
}
